<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图复习-class封装</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .outwrap{
            position: fixed;
            top: 0; bottom: 0;
            left: 0; right: 0;
            margin: auto;
            border: 3px dashed yellowgreen;
            width: 500px;
            height: 300px;
        }
        li{
            height: 100%;
            width: 100%;
            list-style: none;
            position:absolute;
            transition: 1s;
            text-align: center;
            line-height: 300px;
            font-size: 200px;
            opacity: 0;
        }
        button{
            box-sizing: border-box;
            width: 30px;
            height: 50px;
            position: absolute;
            top: 50%;
            background: rgb(89, 104, 94,0.3);
            border: none;
            font-size: 20px;
        }
        .left{
            margin:-15px 0;
        }
        .right{
            right:0;
            margin:-15px 0;
        }
    </style>
</head>
<body>
    <div class="outwrap">
        <ul>
            <li style="background: orange;opacity: 1;">壹</li>
            <li style="background: yellow;">贰</li>
            <li style="background: green;">叁</li>
            <li style="background: skyblue;">肆</li>
            <li style="background: plum;">伍</li>
        </ul>
        <ol></ol>
            <button class="left"> &lt; </button>
            <button class="right"> &gt; </button>
    </div>
    
    

</body>
</html>
<script>
    const outwrap = document.querySelector(".outwrap");
    class banner{
        constructor(outwrap){
            this.ulLis = outwrap.querySelectorAll("ul>li");
            this.left = outwrap.querySelector(".left");
            this.right = outwrap.querySelector(".right");

            this.contentindex = 0;
            this.interval();
            this.mouseEvent();
            this.leftRightClick();
        }

        interval(){
            
            this.id = setInterval(e=>{
                this.ulLis.forEach(item=>item.style.opacity = 0);
                
                ++this.contentindex ;
                if(this.contentindex>=this.ulLis.length){
                    this.contentindex = 0;
                }
                this.ulLis[this.contentindex].style.opacity = 1;
            },1000);
        }

        mouseEvent(){
            outwrap.addEventListener("mouseenter",e=>{
                clearInterval(this.id);
            })
            outwrap.addEventListener("mouseleave",e=>{
                this.interval();
            })
        } 

        leftRightClick(){
            this.left.addEventListener("click",()=>{
                this.ulLis.forEach(item=>item.style.opacity = 0);
                this.contentindex -=1;
                if(this.contentindex<0){
                    this.contentindex = 4;
                }
                this.ulLis[this.contentindex].style.opacity = 1;
            })

            this.right.addEventListener("click",()=>{
                this.ulLis.forEach(item=>item.style.opacity = 0);
                this.contentindex +=1;
                if(this.contentindex>=this.ulLis.length){
                    this.contentindex = 0;
                }
                this.ulLis[this.contentindex].style.opacity = 1;
            })

        }
    }
const res = new banner(outwrap);
   



    
    
</script>